﻿Imports TodoMicroSolutionPackV1.productos.dal.dto
Imports TodoMicroSolutionPackV1.dal.dao
Imports NHibernate

Public Class frmAMBStock
    Private prod As producto
    Private _session As ISession
    Private DAOFactory As NhibernateDAOFactory
    Dim bSrc As New BindingSource

    Protected ReadOnly Property Session As ISession
        Get
            If _session Is Nothing Then
                _session = NhibernateSessionFactory.getSession
            End If
            Return _session
        End Get
    End Property

    Private Sub frmAMBStock_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        If Not _session Is Nothing Then
            _session.Dispose()
        End If
    End Sub

    Private Sub frmAMBStock_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Dispose()
    End Sub
    Private Sub frmAMBStock_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DAOFactory = New NhibernateDAOFactory(Session)
    End Sub

    Private Sub btnCargarProducto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCargarProducto.Click
        prod = comunes.getProducto(Session)
        If Not prod Is Nothing Then
            txtId.Text = prod.id
            txtCodigo.Text = prod.codigo
            txtDescripcion.Text = prod.descripcion
            bSrc.DataSource = prod.stocks
            grdStocks.DataSource = bSrc
        End If
    End Sub

    Private Sub btnInsertar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsertar.Click
        Dim nuevoStock As New stock
        Using tx As ITransaction = Session.BeginTransaction
            nuevoStock.cantidad = CDbl(txtCantidadActual.Text)
            nuevoStock.cantidad_inicial = CDbl(txtCantidadInicial.Text)
            nuevoStock.deposito_id = CInt(txtDeposito.Text)
            nuevoStock.despacho_id = txtNumeroDespacho.Text
            prod.addStock(nuevoStock)
            DAOFactory.getProductoDAO.SaveOrUpdate(prod)
            tx.Commit()
        End Using
        bSrc.ResetBindings(False)
    End Sub

    Private Sub btnModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificar.Click
        Using tx As ITransaction = Session.BeginTransaction
            Dim stock As stock = prod.stocks.Item(grdStocks.CurrentRow.Index)
            stock.cantidad_inicial = CDbl(txtCantidadInicial.Text)
            stock.cantidad = CDbl(txtCantidadActual.Text)
            stock.deposito_id = CInt(txtDeposito.Text)
            stock.despacho_id = txtNumeroDespacho.Text
            bSrc.ResetBindings(False)
            tx.Commit()
        End Using
    End Sub

    Private Sub btnBorrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBorrar.Click
        Using tx As ITransaction = Session.BeginTransaction
            For Each row In grdStocks.SelectedRows
                grdStocks.Rows.Remove(row)
            Next
            tx.Commit()
        End Using
    End Sub

    Private Sub grdStocks_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdStocks.CellContentClick

    End Sub

    Private Sub grdStocks_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdStocks.CellDoubleClick
        Dim stock As stock = prod.stocks.Item(grdStocks.CurrentRow.Index)
        txtCantidadActual.Text = stock.cantidad
        txtCantidadInicial.Text = stock.cantidad_inicial
        txtDeposito.Text = stock.deposito_id
        txtNumeroDespacho.Text = stock.despacho_id
    End Sub
End Class